/*!
 * CodeGenerator(https://git.oschina.net/ptma/CodeGenerator)
 *
 * Date: 2019-06-11 05:48:13
 */
package com.azier.mysql.service;

import com.azier.exception.ServiceException;
import com.azier.mysql.mapper.TransportResourceCoreMapper;
import com.azier.mysql.model.TransportResourceCore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 业务层 <br/>
 * Table: transport_resource_core - 配置信息核心表，包含配置信息的各种重要信息 <br/>
 * 这个
 * @author 金建强(ptma@163.com)
 * @version 2019-06-11 05:48:13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TransportResourceCoreService extends BaseService<TransportResourceCore> {

    @Autowired
    private TransportResourceCoreMapper transportResourceCoreMapper;

    public TransportResourceCore selectBySmId(Long smId){
        Example example = new Example(TransportResourceCore.class);
        example.createCriteria().andEqualTo("smId", smId);
        List<TransportResourceCore> coreList = transportResourceCoreMapper.selectByExample(example);
        if(coreList!=null&&!coreList.isEmpty()){
            return coreList.get(0);
        }
        return null;
    }
    public TransportResourceCore selectByMonitoringId(Long monitoringId){
        Example example = new Example(TransportResourceCore.class);
        example.createCriteria().andEqualTo("monitoringId", monitoringId);
        List<TransportResourceCore> coreList = transportResourceCoreMapper.selectByExample(example);
        if(coreList!=null&&!coreList.isEmpty()){
            return coreList.get(0);
        }
        return null;
    }
    public TransportResourceCore selectByDeviceIdAndConfigIndexAndDataIndex(String deviceId,Short configIndex,Short dataIndex) throws ServiceException {
        Example example = new Example(TransportResourceCore.class);
        example.createCriteria().andEqualTo("deviceId", deviceId)
            .andEqualTo("configIndex",configIndex).andEqualTo("dataIndex",dataIndex);
        List<TransportResourceCore> transportResourceCores = transportResourceCoreMapper.selectByExample(example);
        if(null!=transportResourceCores&&!transportResourceCores.isEmpty()){
            return transportResourceCores.get(0);
        }
        return null;
    }

    public int deleteByDeviceId(String deviceId){
        Example example = new Example(TransportResourceCore.class);
        example.createCriteria().andEqualTo("deviceId", deviceId);
        return transportResourceCoreMapper.deleteByExample(example);
    }
    public int deleteByMonitoringId(Long monitoringId){
        Example example = new Example(TransportResourceCore.class);
        example.createCriteria().andEqualTo("monitoringId", monitoringId);
        return transportResourceCoreMapper.deleteByExample(example);
    }
    public int deleteByDeviceIdAndConfigIndex(String deviceId, Short configIndex){
        Example example = new Example(TransportResourceCore.class);
        example.createCriteria().andEqualTo("deviceId", deviceId)
            .andEqualTo("configIndex", configIndex);
        return transportResourceCoreMapper.deleteByExample(example);
    }
    public int deleteByPage(String deviceId,int page){

        Example example = new Example(TransportResourceCore.class);
        example.createCriteria().andEqualTo("deviceId", deviceId)
            .andBetween("configIndex", page * 10, page * 10 + 9);
        return transportResourceCoreMapper.deleteByExample(example);
    }

    public int updateByDeviceAndConfigIndexOrDataIndex(TransportResourceCore entity) throws ServiceException {
        Example example = new Example(TransportResourceCore.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deviceId",entity.getDeviceId())
            .andEqualTo("configIndex",entity.getConfigIndex());
        if(entity.getDataIndex()!=null){
            criteria.andEqualTo("dataIndex",entity.getDataIndex());
        }

        return transportResourceCoreMapper.updateByExampleSelective(entity,example);
    }

}
